package net.lgpage.tool;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

import java.util.Set;

import com.matriq.cloudmanager.MyApp;

public class SharedPreferencesTool {

	Context context;

	public SharedPreferencesTool(Context context) {
		super();
		this.context = context;
	}

	/**
	 * 
	 * @param key 
	 * @param defValue 基本类型对象
	 * @return 
	 */
	public Object load(String key, Object defValue) {
		return load(MyApp.sharedName, key, defValue);
	}

	public Object userLoad(String key, Object defValue) {
		return load(MyApp.getApp().getLoginObj().getId() + "", key,
				defValue);
	}

	public Object load(String name, String key, Object defValue) {
		if(key == null)return null;
		SharedPreferences sp = context.getSharedPreferences(name, 0);
		if(defValue == null){
			return sp.getString(key,null);
		}
		if (defValue instanceof Integer) {
			return sp.getInt(key, (Integer) defValue);
		} else if (defValue instanceof Long) {
			return sp.getLong(key, (Long) defValue);
		} else if (defValue instanceof String) {
			return sp.getString(key, defValue.toString());
		} else if (defValue instanceof Float) {
			return sp.getFloat(key, (Float) defValue);
		} else if (defValue instanceof Boolean) {
			return sp.getBoolean(key, (Boolean) defValue);
		} else if (defValue instanceof Set) {
			return sp.getStringSet(key, (Set) defValue);
		} else if (defValue instanceof Class) {
			String json = sp.getString(key, null);
			return GsonTool.getGson().fromJson(json, (Class) defValue);
		}else{
			return sp.getString(key,null);
		}

	}

	/*public Object loadClass(String name, String key, Class returnclazz) {
		SharedPreferences sp = context.getSharedPreferences(name, 0);
		String json = sp.getString(key, null);
		if (returnclazz == String.class)
			return json;
		return GsonTool.getGson().fromJson(json, returnclazz);
	}*/

	public void putUserValue(String key, Object value) {
		putValue(MyApp.getApp().getLoginObj().getId() + "", key, value);
	}

	public void putAppValue(String key, Object value) {
		putValue(MyApp.sharedName, key, value);
	}

	public void putValue(String name, String key, Object value) {
		if(key == null)return;
		SharedPreferences appinfo = context.getSharedPreferences(name, 0);
		Editor editor = appinfo.edit();
		if (value == null) {
			editor.remove(key);
		} else {
			if (value instanceof Boolean) {
				editor.putBoolean(key, (Boolean) value);
			} else if (value instanceof Float) {
				editor.putFloat(key, (Float) value);
			} else if (value instanceof Integer) {
				editor.putInt(key, (Integer) value);
			} else if (value instanceof Long) {
				editor.putLong(key, (Long) value);
			} else if (value instanceof String) {
				editor.putString(key, value.toString());
			}else if (value instanceof Set) {
				editor.putStringSet(key, (Set<String>) value);
			}  else {
				editor.putString(key, GsonTool.getGson().toJson(value));
			}
		}
		editor.commit();
	}
}
